//
//  LeftUserHeadCell.swift
//  ams
//
//  Created by coterjiesen on 2017/4/26.
//  Copyright © 2017年 coterjiesen. All rights reserved.
//


import UIKit
import KVOController
import Kingfisher

class UserHeadCell: UITableViewCell {
    /// 头像
    var avatarImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor(white: 0.9, alpha: 0.3)
        imageView.layer.borderWidth = 1.5
        imageView.layer.borderColor = UIColor(white: 1, alpha: 0.6).cgColor
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 32
        return imageView
    }()
    /// 用户名
    var userNameLabel: UILabel = {
        let label = UILabel()
        label.font = v2Font(15)
        return label
    }()
    //identity infomatiom
    var identityLable: UILabel = {
        let label = UILabel()
        label.font = v2Font(17)
        return label
    }()
    
    var formView = UIView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);
        self.setup();
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    func setup()->Void{
        self.backgroundColor = UIColor.clear
        self.selectionStyle = .none
        
        formView.layer.cornerRadius = 5
        formView.backgroundColor = CuColor.colors.v2_backgroundColor

        self.contentView.addSubview(formView)
        formView.addSubview(avatarImageView)
        formView.addSubview(userNameLabel)
        formView.addSubview(identityLable)
        formView.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(6)
            make.right.equalTo(self.contentView).offset(-6)
            make.top.equalTo(self.contentView).offset(3)
            make.bottom.equalTo(self.contentView).offset(-3)
        }
        self.avatarImageView.snp.makeConstraints{ (make) -> Void in
            make.left.equalTo(formView).offset(12)
            make.centerY.equalTo(formView)
            make.width.height.equalTo(self.avatarImageView.layer.cornerRadius * 2)
        }
        self.userNameLabel.snp.makeConstraints{ (make) -> Void in
            make.top.equalTo(self.avatarImageView).offset(8)
            make.left.equalTo(self.avatarImageView.snp.right).offset(20)
        }
        self.identityLable.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.avatarImageView).offset(-3)
            make.left.equalTo(userNameLabel)
        }
        //安全的 KVO（Key-value Observing，键-值 观察）
        self.kvoController.observe(CuUser.sharedInstance, keyPath: "username", options: [.initial , .new]){
            [weak self] (observe, observer, change) -> Void in
            if let weakSelf = self {
                print("CuUser.sharedInstance:\(CuUser.sharedInstance)")
                if let username = CuUser.sharedInstance.username {
                    weakSelf.userNameLabel.text = username
                    weakSelf.avatarImageView.kf.setImage(with: URL(string: "http://cdn-img.easyicon.net/image/mix.png")!, placeholder: nil, options: nil, completionHandler: { (image, error, cacheType, imageURL) -> () in
                        //如果请求到图片时，客户端已经不是登录状态了，则将图片清除
                        if !CuUser.sharedInstance.isLogin {
                            weakSelf.avatarImageView.image = nil
                        }
                    })
                }
                else { //没有登录
                    weakSelf.userNameLabel.text = "请先登录"
                    weakSelf.identityLable.text = "火星管理员"
//                    weakSelf.avatarImageView.image = nil
                    weakSelf.avatarImageView.kf.setImage(with: URL(string: "http://cdn-img.easyicon.net/image/mix.png")!, placeholder: nil, options: nil, completionHandler: nil)
                }
            }
        }
        
        self.thmemChangedHandler = {[weak self] (style) -> Void in
            self?.userNameLabel.textColor = CuColor.colors.v2_TopicListUserNameColor
        }
    }
    func bind(name: String, id: String){
        userNameLabel.text = name
        identityLable.text = id
    }
    
}
